<template>
  <div class="app-container">
    <el-descriptions :title="title" :column="3" border v-if="head != {}">
      <el-descriptions-item v-for="(item,index) in Object.keys(head)" :label="item" label-align="right" v-bind:key="index" >{{form[item]}}</el-descriptions-item>
    </el-descriptions>
    <el-divider></el-divider>
    <el-table :data="bodys" border v-if="bodys.length > 0" >
      <el-table-column v-for="(item,index) in Object.keys(bodys[0])" :label="item" align="center" :prop="item" v-bind:key="index" />
    </el-table>
    <el-divider></el-divider>
    <div v-if="fileName">
      {{fileName}}
<!--      <img :src="'data:image/jpg;base64,'+this.form['文件内容']">-->
      <el-button><a :href="file" :download="fileName">附件下载</a></el-button>
    </div>
    <el-divider></el-divider>
  </div>
</template>

<script>
import Fi from "element-ui/src/locale/lang/fi";
import {Base64} from "js-base64";

export default {
  name: "JiaBan",
  props:{
    form: {
      type: Object,
      default() {
        return {};
      }
    },
    title: {
      type: String,
      default() {
        return "默认表单";
      }
    },
  },
  data() {
    return {
      head:{},
      bodys:[],
      file:'',
      fileName:''
    };
  },
  created() {
    this.head = this.form?{...this.form}:{}
    this.bodys = this.form.content?[...this.form.content]:[]
    delete (this.head['content'])
    delete (this.head['文件内容'])
    delete (this.head['文件名'])
    if (this.form['文件内容'] && this.form['文件名']) {
      this.fileName = this.form['文件名']
      const file = this.base64toFile(this.form['文件内容'], this.form['文件名'])
      this.file = window.URL.createObjectURL(file);
      // if(this.fileName.includes("png")||this.fileName.includes("jpg")||this.fileName.includes("jepg")||this.fileName.includes("png")){
      //   const file = this.base64toFile(this.form['文件内容'])
      //   this.file = window.URL.createObjectURL(file);
      // }else{
      //   const str = Base64.decode(this.form['文件内容']);
      //   const file = new File([str], this.form['文件名'])
      //   this.file = window.URL.createObjectURL(file);
      // }
    }
  },
  methods: {
    base64toFile (dataurl, filename = 'file') {
      let bstr = atob(dataurl)
      let n = bstr.length
      let u8arr = new Uint8Array(n)
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n)
      }
      return new File([u8arr],filename )
    },
  }
};
</script>
